import { CHANGE_BANNERS, CHANGE_RECOMMENDS } from "./constants.js";
import axios from "axios";

export function changeBannersAction(banners) {
  return {
    type: CHANGE_BANNERS,
    payload: banners,
  };
}

export function changeRecommendsAction(recommends) {
  return {
    type: CHANGE_RECOMMENDS,
    payload: recommends,
  };
}

export function fetchDataAction() {
  return (dispatch, getState) => {
    axios.get("http://123.207.32.32:8000/home/multidata").then((res) => {
      const { data } = res?.data || {};
      const { banner, recommend } = data || {};
      const banners = Array.isArray(banner?.list) ? banner?.list : [];
      const recommends = Array.isArray(recommend?.list) ? recommend?.list : [];

      dispatch(changeBannersAction(banners));
      dispatch(changeRecommendsAction(recommends));
    });
  };
}
